package com.hq.zb.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hq.zb.dto.MenuDto;
import com.hq.zb.entity.CurrentUser;
import com.hq.zb.model.Menu;
import com.hq.zb.service.IMenuService;
import com.hq.zb.utils.ContextHolderUtils;
import com.hq.zb.utils.Result;
import com.hq.zb.vo.CommTreeVo;
import com.hq.zb.vo.MenuTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单Controller
 *
 * @author huanghanrui
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/sys")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 分页查询菜单
     * @param menuDto 菜单
     * @param pageNum 账号
     * @param pageSize 密码
     * @return
     */
    @PostMapping("/menu/query")
    @ResponseBody
    public Result queryMenu(@RequestBody MenuDto menuDto, int pageNum, int pageSize) {
        Wrapper<MenuTreeVo> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_flag", false);
        wrapper.like("menu_name", menuDto.getMenuName());
        wrapper.eq("sys_source", menuDto.getSysSource());
        Page<MenuTreeVo> page = new Page<>(pageNum, pageSize);
        Page<MenuTreeVo> list = menuService.queryMenu(page, wrapper);
        return Result.data(list);
    }

    /**
     * 查询菜单树
     * @param menuDto
     * @return
     */
    @PostMapping("/menu/queryTree")
    @ResponseBody
    public Result queryTree(@RequestBody MenuDto menuDto) {
        List<CommTreeVo> menuVos = menuService.queryTree(menuDto);
        String data = JSONObject.toJSONString(menuVos, SerializerFeature.NotWriteDefaultValue);
        return Result.data(JSONObject.parse(data));
    }

    /**
     * 查询用户的菜单
     * @return
     */
    @PostMapping("/menu/queryUserMenu")
    @ResponseBody
    public Result queryByCurrentUser(@RequestBody MenuDto menuDto) {
        //获取当前登陆用户信息
        CurrentUser currentUser = ContextHolderUtils.getCurrentUser();
        List<MenuTreeVo> menuDtos = menuService.queryByCurrentUser(currentUser, menuDto.getSysSource());
        String data = JSONObject.toJSONString(menuDtos, SerializerFeature.NotWriteDefaultValue);
        return Result.data(JSONObject.parse(data));
    }

    /**
     * 保存菜单
     * @param menu
     * @return
     */
    @PostMapping("/menu/save")
    public Result saveMenu(@RequestBody Menu menu) {
        menuService.saveMenu(menu);
        return Result.ok();
    }

    /**
     * 查询菜单列表
     * @param menu 菜单
     * @return
     */
    @PostMapping("/menu/queryList")
    @ResponseBody
    public Result queryMenu(@RequestBody Menu menu) {
        Wrapper<Menu> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_flag", false);
        wrapper.like("menu_name", menu.getMenuName());
        wrapper.eq("sys_source", menu.getSysSource());
        return Result.data(menuService.selectList(wrapper));
    }

    /**
     * 删除菜单
     * @param menus
     * @return
     */
    @PostMapping("/menu/delete")
    public Result deleteMenu(@RequestBody List<Menu> menus) {
        menuService.deleteMenu(menus);
        return Result.ok();
    }
}

